package com.frxuexi.shop2307.shop

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.CommentAdapter
import com.frxuexi.shop2307.databinding.ActivityCommentBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/CommentActivity")
@AndroidEntryPoint
class CommentActivity : BaseActivity<ActivityCommentBinding,ShopViewModel>() {
    val commentAdapter by lazy { CommentAdapter() }
    @JvmField
    @Autowired
    var id:Int=-1

    var page=1
    var type="0"
    override fun initData() {
        vm.commentData.observe(this){
            commentAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.comRv.apply {
            adapter=commentAdapter
            layoutManager= LinearLayoutManager(this@CommentActivity)
        }
        vm.getComment(id,10,1,"0")

        binding.comRg.setOnCheckedChangeListener { group, checkedId ->
            page=1
            when(checkedId){
                R.id.com_all->type="0"
                R.id.com_hao->type="1"
                R.id.com_zhong->type="2"
                R.id.com_cha->type="3"
            }
            vm.getComment(id,10,page,type)
        }

        binding.comSmart.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                vm.getComment(id,10,page,type)
                binding.comSmart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                vm.getComment(id,10,page,type)
                binding.comSmart.finishLoadMore()
            }
        })

        binding.comBack.setOnClickListener {
            finish()
        }
    }

}